package tagsample;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class MyIteratorTag extends TagSupport {

	String[] movies = new String[] {"Spider man", "Saved!", "Amelie"};
	public void setMovies(String[] movies) {
		this.movies = movies;
	}

	int movieCounter;
	
	public int doStartTag() throws JspException{
		movieCounter=0;
		
		pageContext.setAttribute("movie", movies[movieCounter]);
		movieCounter++;
		return EVAL_BODY_INCLUDE;
		
	}
	
	public int doAfterBody() throws JspException {
		if(movieCounter < movies.length){
			pageContext.setAttribute("movie", movies[movieCounter]);
			movieCounter++;
			return EVAL_BODY_AGAIN;
			
		}else {
			return SKIP_BODY;
		}
	
	}
	
	public int doEndTag() throws JspException{
		
		return EVAL_PAGE;
	}

}
